﻿--[[
   @brief    main_web logic for  web_browser
   @author   harleyxwang
   @version  1.0
   @date     2016-01-30
--]]

main_web_control =
{
    init = function()
        base_utility.log("main_web_control.init called");
        ierd_tgp.tpf_ui.Require("main_web\\model.lua");

        main_web_control.get_netbar_flag();
    end;
    
    on_show_login = function()
        -- start init
    end;

    on_login_success = function(event_id, json_param)
        --登录成功，设置uin
        local table_param = base_utility.json_to_table(json_param);
        main_web_model.uin = table_param.uin;
        ierd_tgp.tpf_ui.Log("main_web: on_login_success:uin="..main_web_model.uin);
        main_web_model.init();
    end;

    open_inner_link = function(json)
        local table_param = base_utility.json_to_table(json);
        local link = table_param.link;
        local spilt_url_array = base_utility.split(link, ":");
        if spilt_url_array ~= nil then
            local prefix = spilt_url_array[1];
            local prefix_len = string.len(prefix);
            local suffix = string.sub(link, prefix_len + 2, string.len(link));
            if prefix == 'web' then
                ierd_tgp.tpf_ui.Log("open web");
                local param = {url = suffix};
                event_center.send_event(browser_event.OPEN_TGP_BROWSER,
                    base_utility.table_to_json(param));
            elseif prefix == 'webgame' then
                ierd_tgp.tpf_ui.Log("launch weg game");
                local game_lib =
                    base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
                game_lib:launch_game(tonumber(suffix), 0, 0, 0, "3100081-1",
                    ierd_tgp.game_library.ZONE_TYPE_FORMAL, "");
            end
        end
    end;

    get_netbar_flag = function()
        if main_web_model.is_netbar ~= -1 then
            return main_web_model.is_netbar;
        end;

        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            main_web_model.is_netbar = game_lib:get_bind_channel_id();
        end;
        ierd_tgp.tpf_ui.Log("get_netbar_flag,is_netbar = "..main_web_model.is_netbar);

        return main_web_model.is_netbar;
    end;

    get_language_flag = function()
        if main_web_model.language ~= "-1" then
            return main_web_model.language;
        end;

        main_web_model.language = "";
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            local is_english_ver = tpfui:Is_English_ver();
            if is_english_ver == true then
                main_web_model.language = "en";
            end;
        end;
        return main_web_model.language;
    end;
    
    -- url点击上报
    report_url_click = function(url_type, op_from)
        ierd_tgp.tpf_ui.Log("user_guide_control: report_url_click");
        if url_type == nil or op_from == nil then
            return;
        end;
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        if qosparam ~= nil then
            ierd_tgp.tpf_ui.Log("user_guide_control: report_click url_type"..url_type.."op_from"..op_from);
            qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
            qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick; --url上报
            qosparam.int_count = 3;
            qosparam.str_count = 0; 
            qosparam.int_val[0]= url_type;
            qosparam.int_val[1]= op_from;
            qosparam.int_val[2]= 0;
            base_utility.lua_qos(qosparam);
            qosparam:delete();
        end;
    end;  

    lua_call_JS = function(func_name, json) 
        local nQBWebID = ui_utility.uilib():GetControlIDbyCustomID(CID_MAINFRAME_QBWEB);
        local pControl = ui_utility.uilib():GetControlPtr(nQBWebID);
        local pQBWeb = tolua.cast(pControl, "Tenio::IQBWebBrowser");
        local const_func_name, str_func_name = U8toCUIStr(func_name);
        local const_json_param, str_json_param = U8toCUIStr(json);
        ierd_tgp.tpf_ui.Log("lua_call_JS start.");
        if pQBWeb then
            pQBWeb:ExcecuteJS(const_func_name, const_json_param);
            ierd_tgp.tpf_ui.Log("lua_call_JS success.");
        end;
    end;
    
    on_js_callback = function(uilib, func_name, json)
        ierd_tgp.tpf_ui.Log("on_js_callback.func_name="..func_name);
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_QBWEB);
        local const_func_name, str_func_name = U8toCUIStr(func_name);
        local const_ui_str, str_holder = U8toCUIStr(json);

        if func_name == "jc_set_app_status" or
           func_name == "jc_check_app_status" or
           func_name == "jc_get_window_state" or
           func_name == "jc_get_page_cache_switch" or
           func_name == "jc_open_social_panel" or
           func_name == "tgp_closer" or
           func_name == "tgp_maximize" or
           func_name == "tgp_minimize" or
           func_name == "jc_open_main_menu" or
           func_name == "jc_get_game_qrcode" or
           func_name == "tgp_mover" or 
           func_name == "jc_get_game_st_info" or
           func_name == "i_service_social_get_social_friend_list" or
           func_name == "i_service_social_open_personal_center" or
           func_name == "jc_save_history" or 
           func_name == "jc_go_back" or
           func_name == "jc_web_move_animation_end" then
           local info = {};
           info.func_name = func_name;
           info.json = json;
           event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_MAINFRAME, base_utility.table_to_json(info));
           ierd_tgp.tpf_ui.Log("on_js_callback jmp to mainframe.");
        elseif func_name == "web_mainframe_request_first_info" then
            local param = base_utility.json_to_table(json);
            wegame_mainframe_control.web_mainframe_request_first_info(param);
        elseif func_name == "i_service_social_network_open_msgbox" then
            local param = base_utility.json_to_table(json);
            event_center.send_event(group_event.SHOW_MESSAGE_BOX_DIALOG, base_utility.table_to_json(param.data));
        elseif func_name == "i_service_social_network_get_title_data" then
            comm_center.call_service("i_service_social_network_get_title_ui_data", {});
        elseif func_name == "jc_get_server_state" or 
               func_name == "jc_check_update" or
               func_name == "jc_open_game_path" or
               func_name == "jc_open_post" or 
               func_name == "jc_start_game" or
               func_name == "jc_switch_game" or
               func_name == "jc_install_game" or
               func_name == "jc_update_game" or
               func_name == "jc_cancel_update_game" or
               func_name == "jc_cancel_update_plugin" or
               func_name == "jc_cancel_download" or
               func_name == "jc_stop_game" or
               func_name == "jc_continue_download_game" or
               func_name == "jc_continue_update_game" or
               func_name == "jc_continue_update_plugin" or
               func_name == "jc_download_game" or
               func_name == "jc_pause_download_game" or
               func_name == "jc_pause_update_game" or
               func_name == "jc_pause_update_plugin" or
               func_name == "jc_update_plugin" then
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_WEB_BOTTOM, base_utility.table_to_json(info));
            ierd_tgp.tpf_ui.Log("on_js_callback jmp to web_bottom!");
        elseif func_name == "jc_get_download_gift" then
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_FRAME_BOTTOM,
                base_utility.table_to_json(info));
        elseif func_name == "jc_batch_get_game_state_info" then
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_GAME_LIBRARY,
                base_utility.table_to_json(info));
        elseif func_name == "m_add_wandering_game" or
               func_name == "m_remove_wandering_game" or
               func_name == "jc_get_installed_games" then
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_GAME_MGR,
                base_utility.table_to_json(info));       
        elseif func_name == "m_largegame_func" then
            event_center.send_event(extendboard_event.ON_JS_CALLBACK_GL, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_largegame_func!");
        elseif func_name == "m_query_game_info_by_id" then
            event_center.send_event(extendboard_event.QUERY_GAME_INFO_BY_ID, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_game_info_by_id!");
        elseif func_name == "m_query_play_game_list" then
            event_center.send_event(extendboard_event.QUERY_PLAY_GAME_LIST, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_play_game_list!");
        elseif func_name == "m_query_attent_game_list" then
            event_center.send_event(extendboard_event.QUERY_ATTENT_GAME_LIST, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_attent_game_list!");
        elseif func_name == "m_query_nearest_game_list" then
            event_center.send_event(extendboard_event.QUERY_NEAREST_GAME_LIST, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_nearest_game_list!");
        elseif func_name == "m_query_rec_game_name" then
            event_center.send_event(extendboard_event.QUERY_REC_GAME_NAME, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_rec_game_name!");
        elseif func_name == "m_query_expect_game_rank" then
            event_center.send_event(extendboard_event.QUERY_EXPECT_GAME_RANK, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_query_expect_game_rank!");
        elseif func_name == "m_webgame_func" then
            ierd_tgp.tpf_ui.Log("on_js_callback m_webgame_func!");
            event_center.send_event(extendboard_event.ON_JS_CALLBACK_WEB_GMAE, json);
        elseif func_name == "m_check_is_in_game_list" then
            base_utility.log("m_check_is_in_game_list");
            event_center.send_event(extendboard_event.CHECK_IS_IN_GAME_LIST, json);
        elseif func_name == "m_check_is_in_visible_game_list" then
            base_utility.log("m_check_is_in_visible_game_list");
            event_center.send_event(extendboard_event.CHECK_IS_IN_VISIBLE_GAME_LIST, json);
        elseif func_name == "m_add_web_game" then
            base_utility.log("m_add_web_game");
            event_center.send_event(extendboard_event.ADD_WEB_GAME, json);
        elseif func_name == "m_add_mobile_game_assistant" then
            base_utility.log("m_add_mobile_game_assistant");
            event_center.send_event(extendboard_event.ADD_MOBILE_GAME_ASSISTANT, json);
        elseif func_name == "m_check_has_mobile_game_assistant" then
            base_utility.log("m_check_has_mobile_game_assistant");
            event_center.send_event(extendboard_event.CHECK_HAS_MOBILE_GAME_ASSISTANT, json);
        elseif func_name == "m_launch_web_game" then
            base_utility.log("m_launch_web_game");
            event_center.send_event(extendboard_event.LAUNCH_WEB_GAME, json);
        elseif func_name == "m_mobilegame_func" then
            ierd_tgp.tpf_ui.Log("on_js_callback m_mobilegame_func!");
            event_center.send_event(extendboard_event.ON_JS_CALLBACK_MOBILE_GMAE, json);
        elseif func_name == "m_gift_func" then
            event_center.send_event(extendboard_event.ON_JS_CALLBACK_GC, json);
            ierd_tgp.tpf_ui.Log("on_js_callback m_gift_func!");
        elseif func_name == "m_personal_center_func" then
            ierd_tgp.tpf_ui.Log("on_js_callback m_personal_center_func!");
        elseif func_name == "m_get_netbar_flag" then
            base_utility.log("[m_get_netbar_flag]");
            main_web_control.handle_get_netbar_flag(json);
        elseif func_name == "m_set_module_color_and_image" then
            base_utility.log("[m_set_module_color_and_image]");
            banner_control.set_module_color_and_image(json);
        elseif func_name == "m_change_client_color_and_image" then
            base_utility.log("[m_change_client_color_and_image]");
            banner_control.change_client_color_and_image(json);
        elseif func_name == "jc_personal_info" then
            event_center.send_event(personal_center_event.JC_PERSONAL_INFO, json);
        elseif func_name == "jc_player_info" then
            event_center.send_event(personal_center_event.JC_PLAYER_INFO, json);
        elseif func_name == "jc_sign_in_info" then
            event_center.send_event(personal_center_event.JC_SIGN_IN_INFO, json);
        elseif func_name == "jc_sign_in" then
            event_center.send_event(personal_center_event.JC_SIGN_IN, json);
        elseif func_name == "jc_task_com_info" then
            event_center.send_event(personal_center_event.JC_TASK_COM_INFO, json);
        elseif func_name == "jc_task_list" then
            event_center.send_event(personal_center_event.JC_TASK_LIST, json);
        elseif func_name == "jc_holly_wood_reward_list" then
            event_center.send_event(personal_center_event.JC_HOLLY_WOOD_REWARD_LSIT, json);
        elseif func_name == "jc_blue_diamond_coupon" then
            event_center.send_event(personal_center_event.JC_BLUE_DIAMOND_COUPON, json);
        elseif func_name == "jc_holly_wood_chk_flag" then
            event_center.send_event(personal_center_event.JC_HOLLY_WOOD_CHK_FLAG, json);
        elseif func_name == "jc_blue_diamond_chk_flag" then
            event_center.send_event(personal_center_event.JC_BLUE_DIAMOND_CHK_FLAG, json);
        elseif func_name == "jc_individual_center_my_privilege" then
            event_center.send_event(personal_center_event.JC_INDIVIDUAL_CENTER_MY_PRIVILEGE, json);
        elseif func_name == "jc_personal_center_open" then
            event_center.send_event(personal_center_event.JC_PERSONAL_CENTER_OPEN, json);        
        elseif func_name == "open_privilege_change_window" then
            event_center.send_event(personal_center_event.OPEN_PRIVILEGE_CHANGE_WINDOW, json);
        elseif func_name == "close_levelup_tips" then
            event_center.send_event(personal_center_event.CLOSE_LEVELUP_TIPS, json);
        elseif func_name == "close_gameend_tips" then
            event_center.send_event(personal_center_event.CLOSE_GAMEEND_TIPS, json);
        elseif func_name == "jc_get_install_state" then
            event_center.send_event(personal_center_event.JS_GET_INSTALL_STATE, json);
        elseif func_name == "jc_first_open_detail" then
            event_center.send_event(extendboard_event.JC_GET_FIRST_OPEN_DETAIL, json);
        elseif func_name == "get_game_states" then
            event_center.send_event(game_library_event.GET_GAME_STATES, json);
        elseif func_name == "open_link" then
            local paramTable = base_utility.json_to_table(json);
            local param = {};
            if paramTable["link"] == "setting:0" then
                local str = {tab_id = 0};
                event_center.send_event(main_menue_event.MAIN_MENUE_SETTING, base_utility.table_to_json(str));
            elseif paramTable["link"] == "gamelib:0" then
                param.type = "mmo_game";
                web_utility.open_link(base_utility.table_to_json(param));
            elseif paramTable["link"] == "gift_exchange:0" then
                param.type = "gift";
                web_utility.open_link(base_utility.table_to_json(param));
            elseif paramTable["link"] == "web_browser" then
                param.type = "open_web";
                param.browser = "new";
                param.url = paramTable.url;
                param.title = paramTable.title or paramTable.url;
                param.use_ptlogin = 1;
                web_utility.open_link(base_utility.table_to_json(param));
            else
            end;
        elseif func_name == "open_blue_diamond" then
            local info = {};
            local url = LoadStr("OPEN_DIAMOND_URL");
            if tonumber(personal_center_model.uin) > 0 then
                local uin = personal_center_model.uin;
                info.url = web_utility.make_login_status_url(url, uin);
                info.width = 490;
                info.height = 465;
                event_center.send_event(browser_event.OPEN_TGP_BROWSER, base_utility.table_to_json(info));
            end;
        elseif func_name == "pc_get_route_vendor" then
            event_center.send_event(personal_center_event.JC_ROUTER_VENDOR_INFO, json);
        elseif func_name == "check_qm_software_env" then
            event_center.send_event(personal_center_event.CHECK_QM_SOFTWARE_ENV, json);
        elseif func_name == "jc_qm_software_chk_flag" then
            event_center.send_event(personal_center_event.JC_QM_SOFTWARE_CHK_FLAG, json);
        elseif func_name == "jc_mobile_tgp_chk_flag" then
            event_center.send_event(personal_center_event.JC_MOBILE_TGP_CHK_FLAG, json);
        elseif func_name == "jc_qm_software_reward" then
            event_center.send_event(personal_center_event.JC_QM_SOFTWARE_REWARD, json);
        elseif func_name == "jc_mobile_tgp_reward" then
            event_center.send_event(personal_center_event.JC_MOBILE_TGP_REWARD, json);
        elseif func_name == "open_qm_url" then
            local url = LoadStr("GUANJIA_URL");
            web_utility.open_browser(url, false, true);
            -- 30代表管家推荐链接，3代表点击来自特权中心（1.mini下载器2.新手引导3.特权中心）
            -- 具体可参考tgp客户端数据上报定义.xls.xml，3405
            main_web_control.report_url_click(30, 3);
        elseif func_name == "open_holly_wood" then
            local info = {};
            local url = LoadStr("OPEN_HOLLYWOOD_URL");
            if tonumber(personal_center_model.uin) > 0 then
                local uin = personal_center_model.uin;
                info.url = web_utility.make_login_status_url(url, uin);
                info.width = 690;
                info.height = 505;
                event_center.send_event(browser_event.OPEN_TGP_BROWSER, base_utility.table_to_json(info));
            end;
        elseif func_name == "jc_privilege_change_info" then
            event_center.send_event(personal_center_event.JC_PRIVILEGE_CHANGE_INFO, json);
        elseif func_name == "jc_gameover_privilege_info" then
            event_center.send_event(personal_center_event.JC_GAMEOVER_PRIVILEGE_INFO, json);
        elseif func_name == "jc_open_gamelibrary" then
            -- if extendboard_model.open_page == "mmo_game" then
                event_center.send_event(extendboard_event.OPEN_GAME_LIST, 
                    base_utility.table_to_json(main_web_model.large_game_nav_param));
            -- end;
        elseif func_name == "jc_update_gamelibrary_alpha" then
            event_center.send_event(extendboard_event.UPDATE_GAMELIBRARY_ALPHA, json);
        elseif func_name == "jc_open_gamedetail" then
            event_center.send_event(extendboard_event.OPEN_GAME_DETIAL, json);
        elseif func_name == "jc_show_custom_cover_tips" then
            event_center.send_event(extendboard_event.SHOW_CUSTOM_COVER_TIPS, json);
        elseif func_name == "jc_open_gift_center" then
            if main_web_model.open_page == "gift" then
                web_utility.lua_call_js("cj_open_gift_center", base_utility.table_to_json(main_web_model.gift_nav_param), 
                    CID_MAINFRAME_QBWEB);
            end;           
        elseif func_name == "jc_open_priv" then
            if main_web_model.open_page == "personal_center" then
                web_utility.lua_call_js("cj_open_priv",base_utility.table_to_json(main_web_model.personal_center_nav_param),
                    CID_MAINFRAME_QBWEB);
            end;
        elseif func_name == "open_browser" then
            local lua_table = nil;
            if json ~= nil and json ~= "" then
                lua_table = base_utility.json_to_table(json);
            end;
            if nil == lua_table then return end
            local info = {};
            info.url = web_utility.make_login_status_url(lua_table.url, main_web_model.uin);
            info.width = lua_table.width;
            info.height = lua_table.height;
            event_center.send_event(browser_event.OPEN_TGP_BROWSER, base_utility.table_to_json(info));
        elseif func_name == "open_dirui" then
            event_center.send_event(open_link_event.OPEN_DIRUI, json);
        elseif func_name == "select_zone" then
            main_web_control.handle_select_zone(json);
        elseif func_name == "jc_query_game_info" or
               func_name == "jc_query_goods_price" then
               local info = {};
               info.func_name = func_name;
               info.json = json;
               event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_RAIL_PROXY, base_utility.table_to_json(info));
        elseif func_name == "jc_show_dlc_btn" then
            local json_tb = base_utility.json_to_table(json);
            local show_dlc_btn = false;
            if json_tb.hasDlcList ~= nil and json_tb.hasDlcList == true then
                show_dlc_btn = true;
            end;

            base_utility.log("[jc_show_dlc_btn], show_dlc_btn = ".. tostring(json_tb.hasDlcList));
            frame_bottom_view.show_dlc_btn(show_dlc_btn);
        elseif func_name == "jc_show_dlc_mgr" then
            base_utility.log("[jc_show_dlc_mgr] web show dlc mgr");
            frame_bottom_view.show_dlc_mgr();
        elseif func_name == "jc_show_order_tips" then
            event_center.send_event(popup_wnd_event.SHOW_NETBAR_ORDER_TIPS, json);
        elseif func_name == "switch_multi_account_list_dlg" then
            event_center.send_event(
                mult_account_event.MULT_ACCOUNT_SWITCH_ACCOUNT_LIST_DLG, json);
        elseif func_name == "social_update_msg_event" or
               func_name == "jc_set_social_tip" or
               func_name == "jc_close_social_ad_tips" then
            --社交相关
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(social_event.WEB_CALL_SOCIAL, base_utility.table_to_json(info));
        elseif func_name == "get_friend_list" then
            event_center.send_event(
                group_event.OTHER_MODULE_GET_FRIEND_LIST, json);
        elseif func_name == "jc_get_all_game_list" then
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                if false == main_web_model.game_base_info_ready then
                    main_web_control.init_game_base_info();
                    main_web_model.game_base_info_ready = true;
                end
                local info = {};
                info.result = 0;
                info.seq = base_utility.json_to_table(json).seq;
                info.data = {};
                info.data.length = main_web_model.game_base_info_list.count;

                local i = 0;
                for i = 0, main_web_model.game_base_info_list.count - 1, 1 do
                    info.data[i] = {};
                    info.data[i].game_id = main_web_model.game_base_info_list.game_base_info[i].id;
                    info.data[i].name = main_web_model.game_base_info_list.game_base_info[i].name;
                    info.data[i].type = main_web_model.game_base_info_list.game_base_info[i].main_type;
                    info.data[i].sub_type = main_web_model.game_base_info_list.game_base_info[i].type;
                    info.data[i].release_date = main_web_model.game_base_info_list.game_base_info[i].release_date;
                    info.data[i].tags = main_web_model.game_base_info_list.game_base_info[i].tags;
                end

                main_web_control.lua_call_JS("cj_get_all_game_list", base_utility.table_to_json(info));
            end;
        elseif func_name == "cache_boutique_cfg" then
            local param = base_utility.json_to_table(json);
            main_web_model.game_tags = param;
        elseif func_name == "set_boutique_cfg" then
            base_utility.log(string.format("[query_play_game_list]json:%s", json));
            local param = base_utility.json_to_table(json);
            main_web_model.game_tags = param;
            main_web_control.set_boutique_cfg(param);
        elseif func_name == "get_boutique_cfg" then
            base_utility.log(string.format("[query_play_game_list]json:%s", json));
            local param = base_utility.json_to_table(json);
            if nil ~= param and
                param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_info = {};
                local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
                rsp_info.rsp_table = rsp_table;
                rsp_info.callback_func = param.callback_func;

                local social_network =
                    base_utility.get_component_adapter("ISocial_network",
                        "ierd_tgp.social_network.Get_social_network_adapter();");
                if social_network ~= nil then
                    local seq = param.seq_num or '0';
                    local key = param.data.key or 'Boutique';
                    main_web_control.push_get_boutique_cfg_rsp_info(rsp_info);
                    social_network:get_boutique_cfg(key, seq);
                end
            end

        elseif func_name == "open_my_assets" then
            local tb = base_utility.json_to_table(json);
            if tb.data ~= nil then
                local param = {
                    tipsIcon = "",
                    cancelBtn = {
                        visible = false,
                        text = LoadStr("WEB_OPEN_MY_ASSETS_CANCEL"),
                        handle = function()
                        end
                    },
                    okBtn = {
                        visible = true;
                        text = LoadStr("WEB_OPEN_MY_ASSETS_OK"),
                        handle = function()
                            poptips_control.close_tips();
                        end
                    }
                };
                param.desc = tb.data.text;
                ui_utility.show_tips(param);
            end
        elseif func_name == "jc_get_marquee_info" then
            event_center.send_event(banner_event.GET_GC_MARQUEE_INFO, json);
        elseif func_name == "m_open_add_local_game_wnd" then
            event_center.send_event(main_menue_event.MAIN_MENUE_ADD_GAME, "");
        elseif func_name == "m_game_keeper_my_local_game" then
            event_center.send_event(game_keeper_event.GAME_KEEPER_ON_JS_CALLBACK_LOCAL_GAME_PAGE, json);
        -- 给其他模块处理，比如CF助手
        elseif func_name == "jc_get_user_signature_and_area_info" then
            event_center.send_event(personal_center_event.JS_GET_USER_SIGNATURE_AND_AREA_INFO, json);
        elseif func_name == "jc_save_user_signature_and_area_info" then
            event_center.send_event(personal_center_event.JS_SAVE_USER_SIGNATURE_AND_AREA_INFO, json);
        elseif func_name == "jc_open_hardware_check" then
            local json_table = nil;
            if json ~= nil and json ~= "" then
                json_table = base_utility.json_to_table(json);
            end;
            if nil == json_table then
                return;
            end;
            if json_table.data == nil then
                return;
            end;

            if json_table.data.game_id == nil then
                return;
            end;

            local param = {};
            param.type = "tools";
            param.sub_page = "HARDWARE_CHECK";
            param.game_id = json_table.data.game_id;
            web_utility.open_link(base_utility.table_to_json(param));
        elseif func_name == "jc_hc_request_check" then
            local new_param = {fuc_name = func_name, json = json};
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_HC,
                base_utility.table_to_json(new_param));
        elseif func_name == "jc_get_buy_game_oper_setting" then
            main_web_control.handle_get_buy_game_oper_setting(json);
        elseif func_name == "jc_get_web_https_gray" then
            main_web_control.handle_get_web_https_gray(json);
        elseif func_name == "jc_refresh_page" then
            web_operation.handle_refresh_page(json);
        else
            local info = {};
            info.func_name = func_name;
            info.json = json;
            event_center.send_event(main_web_event.ON_JS_CALLBACK_TO_HELPER, base_utility.table_to_json(info));
        end;

    end;

    -- tgp退出或切换
    do_tgp_close = function(event_id, json)
        local param = main_web_model.game_tags;
        if param ~= nil then
            for i = 1, #param.data.tags do
                local game = param.data.tags[i];
                if game.isnew then
                    game.isnew = false;
                end
            end
            main_web_control.set_boutique_cfg(param);
        end
    end;

    -- 设置精选页游戏字段
    set_boutique_cfg = function(param)
        if nil ~= param and nil ~= param.data and
            param.callback_func ~= nil and param.callback_func ~= "" then
            local rsp_info = {};
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_info.rsp_table = rsp_table;
            rsp_info.callback_func = param.callback_func;

            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            if social_network ~= nil then
                main_web_control.push_set_boutique_cfg_rsp_info(rsp_info);
                local key = param.data.key or 'Boutique';
                social_network:set_boutique_cfg(key, base_utility.table_to_json(param.data));
                local temp_data = main_web_model.set_boutique_rsp_info;
                if temp_data ~= nil then
                    local rsp_json = base_utility.table_to_json(temp_data);
                    main_web_control.on_set_boutique_cfg_result(rsp_json)
                end
            end
        end
    end;

    -- 获取跑马灯数据返回
    send_marquee_info = function(event_id, json)
        local info = base_utility.json_to_table(json);
        info.result = 0;
        local json_str = base_utility.table_to_json(info);
        base_utility.log("[main_web_control]send_marquee_info, marquee_list"..json_str);
        main_web_control.lua_call_JS("cj_get_marquee_info", json_str);
    end;

    handle_get_netbar_flag = function(json)
        base_utility.log(string.format(
            "[main_web_control][handle_get_netbar_flag]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local netbar_flag = 0;
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                netbar_flag = game_lib:get_bind_channel_id();
            end
            base_utility.log(string.format(
                "[main_web_control][handle_get_netbar_flag]net_bar_flag:%d", netbar_flag));
            rsp_table.data = {data_netbar_flag = netbar_flag};   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    handle_get_client_info = function(json)
        base_utility.log(string.format(
            "[main_web_control][handle_get_client_info]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local netbar_flag = 0;
            local ver = 0;
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                netbar_flag = game_lib:get_bind_channel_id();
                ver = game_lib:get_tgp_version_num();
            end
            local bar_id = 0;
            local netbar_tool_id = 0;
            local web_st = "";
            local pkey_str = "";
            local netbar_mgr =
                base_utility.get_component_adapter("INet_bar",
                    "ierd_tgp.net_bar.GetNetBarModule();");
            if netbar_mgr ~= nil then
                bar_id = netbar_mgr:get_bar_info().id;
                netbar_tool_id = netbar_mgr:get_netbar_tool_id();
                local login_mgr =
                    base_utility.get_component_adapter("ILogin_mgr",
                        "ierd_tgp.login_mgr.GetLoginMgr();");
                if login_mgr ~= nil then
                    local info = ierd_tgp.login_mgr.WebStPod:new();
                    login_mgr:GetWebSt(info);
                    web_st = info.web_st;
                    info:delete();
                    local client_st_info = ierd_tgp.login_mgr.ClientStPod:new();
                    login_mgr:GetClientSt(client_st_info);
                    pkey_str = client_st_info.client_st;
                    client_st_info:delete();
                end
            end
            base_utility.log(string.format(
                "[main_web_control][handle_get_netbar_flag]net_bar_flag:%d", netbar_flag));
            rsp_table.data = {uin = login_model.main_uin,
                is_netbar = netbar_flag, version = ver, pkey = pkey_str,
                bar_id = bar_id, netbar_tool_id = netbar_tool_id, web_st = web_st};   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    send_friend_list = function(event_id, json_str)
        web_utility.lua_call_js_ex("get_friend_list", json_str, CID_MAINFRAME_QBWEB);
    end;

    handle_select_zone = function (json)
        if nil == json or "" == json then
            return;
        end;
        local lua_table = base_utility.json_to_table(json);
        if nil == lua_table then
            return;
        end;

        if dirui_control.is_web_game(lua_table.data.game_id) then
            dirui_control.change_selected_server_webgame(lua_table.data.game_id,
            lua_table.data.zone_id, lua_table.data.server_name, lua_table.data.status);
        else
            dirui_control.change_selected_server(lua_table.data.game_id, lua_table.data.zone_id);
        end;
        local complete_info = web_utility.web_rsp_comm_data(lua_table, 0, "ok");
        complete_info.data = lua_table.data;
        web_utility.lua_call_js_ex(lua_table.callback_func, base_utility.table_to_json(complete_info), CID_MAINFRAME_QBWEB);
    end;

    on_get_boutique_cfg_result = function (event_id, json)
        if nil ~= json then
            local rsp_info = main_web_control.pop_get_boutique_cfg_rsp_info();
            if rsp_info ~= nil and 
               rsp_info.callback_func ~= nil  and
               rsp_info.rsp_table ~= nil then
                local param = base_utility.json_to_table(json);
                rsp_info.rsp_table.seq_num = param.seq_num;
                rsp_info.rsp_table.data = param;
                local rsp_json = base_utility.table_to_json(rsp_info.rsp_table);
                web_utility.lua_call_js_ex(rsp_info.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    on_set_boutique_cfg_result = function (event_id, json)
        if nil ~= json_param then
            local rsp_info = main_web_control.pop_set_boutique_cfg_rsp_info();
            if rsp_info ~= nil and 
               rsp_info.callback_func ~= nil  and
               rsp_info.rsp_table ~= nil then
                main_web_model.set_boutique_rsp_info = base_utility.json_to_table(json);
                rsp_info.rsp_table.data = main_web_model.set_boutique_rsp_info;
                local rsp_json = base_utility.table_to_json(rsp_info.rsp_table);
                web_utility.lua_call_js_ex(rsp_info.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    push_set_boutique_cfg_rsp_info = function(rsp)
        local rsp_json = base_utility.table_to_json(rsp);
        base_utility.log("push_set_boutique_cfg_rsp_info, rsp: "..rsp_json);
        if rsp == nil then
            base_utility.log("push_set_boutique_cfg_rsp_info, rsp is error!");
            return;
        end
        main_web_model.set_boutique_rsp_info_queue = rsp;
    end;

    pop_set_boutique_cfg_rsp_info = function()
        local rsp = main_web_model.set_boutique_rsp_info_queue;
        return rsp;
    end;

    push_get_boutique_cfg_rsp_info = function(rsp)
        local rsp_json = base_utility.table_to_json(rsp);
        base_utility.log("push_get_boutique_cfg_rsp_info, rsp: "..rsp_json);
        if rsp == nil then
            base_utility.log("push_get_boutique_cfg_rsp_info, rsp is error!");
            return;
        end
        main_web_model.get_boutique_rsp_info_queue = rsp;
    end;

    pop_get_boutique_cfg_rsp_info = function()
        local rsp = main_web_model.get_boutique_rsp_info_queue;
        return rsp;
    end;

    init_game_base_info = function()
        main_web_control.update_game_base_info();
        main_web_model.game_base_info_ready = true;
    end;

    update_game_base_info = function()
        event_center.send_event(banner_event.UPDATE_GAME_BASE_INFO, "");
    end;
    
    on_before_download = function(uilib, download_url, file_path)
        ierd_tgp.tpf_ui.Log("on_before_download: download_url="..download_url.."file_path="..file_path);
        --local const_json_str = base_utility.table_to_json('1');
        web_utility.lua_call_js_ex('tooglePayCompDownloadLayer', 1, CID_MAINFRAME_QBWEB);
    end;
    
    on_download_completed = function(uilib, download_url, file_path, result)
        ierd_tgp.tpf_ui.Log("on_download_completed: download_url="..download_url.."file_path="..file_path.."result="..result);
        web_utility.lua_call_js_ex('tooglePayCompDownloadLayer', 0, CID_MAINFRAME_QBWEB);
        local is_download_file_exist = base_utility.is_file_exist(file_path);
        if result == 1 and
            is_download_file_exist == true and
            string.find(file_path, "tenpaycert") ~= nil then
                strCmd = 'start "" '..file_path;
                os.execute(strCmd);
        end;
    end;

    on_iframe_before_nav = function(uilib, url ,ctrl_id)
        if web_operation ~= nil then
            web_operation.error_url = "";
            if string.find(url, "loadFailed") ~= nil then
                web_operation.error_url = url;
            end
        end;
    end;

    handle_get_buy_game_oper_setting = function (json_str)
        base_utility.log(string.format("[main_web_control][handle_get_buy_game_oper_setting]json:%s", json_str));
        local param = base_utility.json_to_table(json_str);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local setting_info = {common = 0, key = "CfgMgr.UserSysSetting.BuyGameOper"};
            local setting_data = comm_center.call_service("Svr_GetConfig", setting_info);
            local setting_value = 1;
            if 0 ~= setting_data.exits then
                setting_value = setting_data.value;
            end
            rsp_table.data = {buy_game_oper_seting = setting_value};   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;
    
    -- data_in : {'page_info':{'id':number, 'url':string}, 
    --            'page_param': {'key': string , 'data' : table}}
    open_page_req = function(data_in)
        if data_in == nil or data_in.page_info == nil then
            base_utility.log("open_page : error param!");
            return;
        end;

        local jump_info = {};
        jump_info.type = "jump_url";
        jump_info.id = GAME_ID.GAEM_ID_HOME_PAGE;
        jump_info.url = "";

        if data_in.page_info.id ~= nil then
            jump_info.id = tonumber(data_in.page_info.id);
        end;
        
        if data_in.page_info.url ~= nil then
            jump_info.url = data_in.page_info.url;
        else
            local module_info = banner_model.get_banner_module_info_by_game_id(jump_info.id);
            if module_info ~= nil and module_info.module_url ~= nil then
                jump_info.url = module_info.module_url;
            end;
        end;
        
        if jump_info.url == "" then
            base_utility.log("open_page url empty!");
            return;
        end;
        
        if wegame_mainframe_view.is_closed == false then
            wegame_mainframe_view.show(false);
        end;
        web_utility.open_link_convert(base_utility.table_to_json(jump_info));
        
        if data_in.page_param == nil or type(data_in.page_param) ~= 'table' then
            return;
        end;

        local page_param = base_utility.copy_table(data_in.page_param);
        if page_param == nil or page_param.key == nil then
            base_utility.log("page_param invaild");
            return;
        end;

        main_web_model.page_load_param[tostring(page_param.key)] = page_param;

        comm_center.send_broadcast('i_broadcast_open_page', page_param);
    end;

    pop_page_param_req = function(data_in)
        if data_in ~= nil and data_in.key ~= nil and 
           main_web_model.page_load_param[data_in.key] ~= nil then
            local data = base_utility.copy_table(main_web_model.page_load_param[data_in.key]);
            main_web_model.page_load_param[data_in.key] = nil;
            return data;
        else
            return {};
        end;
    end;
    
    -- 获取机器环境信息的接口，这里放置用户无关的信息
    -- data_in : {'language' : string, 'is_netbar' : int}
    get_client_env = function(data_in)
        if data_in == nil or type(data_in) ~= 'table' then
            base_utility.log("get_env_info : error param!");
            return;
        end;

        -- 语言环境
        data_in.language = main_web_control.get_language_flag();
        
        -- 网吧环境
        data_in.is_netbar = main_web_control.get_netbar_flag();
    end;
    
    handle_get_web_https_gray = function(json)
        base_utility.log(string.format(
            "[main_web_control][handle_get_web_https_gray]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local web_https_gray = false;

            local feature_mgr = base_utility.get_component_adapter("IFeature_mgr",
                                "ierd_tgp.feature.GetFeatureMgrAdapter();");
            if feature_mgr ~= nil then
                web_https_gray = feature_mgr:support_feature(ierd_tgp.feature.web_https_gray);
            end

            base_utility.log(string.format(
                "[main_web_control][handle_get_web_https_gray]web_https_gray:%s", tostring(web_https_gray)));
            rsp_table.data = {web_https_gray = web_https_gray};   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
            end
        end
    end;

    on_tgp_st_renewed = function(event_id, json_str)
        base_utility.log("[main_web_control][on_tgp_st_renewed]" .. json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if nil == param_tbl then
            base_utility.log("[main_web_control][on_tgp_st_renewed] param_tbl is nil");
            return;
        end;
        -- cookie更新，pkey, web_st
        local pkey = "";
        local web_st = "";
        local uin = 0;
        pkey = param_tbl["st"];
        uin = param_tbl["uin"];
        local tgp_id = param_tbl["tgp_id"];
        local tgp_ticket = param_tbl["web_token"];
        local tgp_biz_ticket = param_tbl["biz_ticket"];

        local user_region = "";
        local region_tbl = comm_center.call_service("Svr_GetUserSelfRegion", {});
        if region_tbl ~= nil and region_tbl.user_region ~= nil then
            user_region = region_tbl.user_region;
        end

        local login_mgr = base_utility.get_component_adapter("ILogin_mgr", 
                            "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr ~= nil then
            local info = ierd_tgp.login_mgr.WebStPod:new();
            login_mgr:GetWebSt(info);
            web_st = info.web_st;
            info:delete();
        end;

        local callback_func = web_register_center.get_func_by_browser(
                "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            rsp_data_table.data.pkey = pkey;
            rsp_data_table.data.web_st = web_st;
            rsp_data_table.data.uin = uin;
            rsp_data_table.data.tgp_id = tgp_id;
            rsp_data_table.data.tgp_ticket = tgp_ticket;
            rsp_data_table.data.custom_cmd = "tgp_st_renewed";
            rsp_data_table.data.tgp_biz_ticket = tgp_biz_ticket;
            rsp_data_table.data.region = user_region;
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
    end;
};

main_web_control.init();
